Tuples¶

🎨 float¶

In [1]:
number = 3.1415
In [2]:
type(number)
Out[2]:
float
In [5]:
float('3.5')
Out[5]:
3.5
In [6]:
float('3.1415') * 2
Out[6]:
6.283

🎨 round¶

In [7]:
round(3.14159, 2)
Out[7]:
3.14
In [8]:
round(3.14159, 4)
Out[8]:
3.1416
In [9]:
apples = 7
people = 3
per_person = apples / people
print(f'Each person gets { per_person } apples')
Each person gets 2.3333333333333335 apples
🤨
In [11]:
apples = 7
people = 3
per_person = apples / people
print(f'Each person gets { round(per_person, 1) } apples')
Each person gets 2.3 apples

🎨 None¶

What is the value of a variable that doesn't have a value?

What does a function return when it doesn't return anything?

In [12]:
def no_return():
    print('This function doesn\'t return anything.')
    

value = no_return()

print(value)
This function doesn't return anything.
None
In [13]:
type(value)
Out[13]:
NoneType

None is what Python uses to communicate nothing.

It's what you use to indicate that you don't have any information.

In [14]:
thing = 8
print(thing == 8)
print(thing is None)
True
False
In [15]:
thing = None
thing is None
Out[15]:
True

To determine whether a variable is None, use the is None expression.

In [16]:
thing = 9
thing is not None
Out[16]:
True

To determine whether a variable is not None, use the is not None expression.

🎨 tuple¶

In [17]:
info = ('Gordon', 'Bean', 39)
In [18]:
print(info)
('Gordon', 'Bean', 39)
In [19]:
type(info)
Out[19]:
tuple

A tuple is a collection of data, like a list.

A list stores a sequence of data that has the same role or quality.

  • e.g. Everyone's first name
    ['John', 'Mary', 'Susan', 'David']
    

A tuple stores distinct pieces of information that belong together as a larger unit.

  • e.g. The first name, last name, and age of a single person
    ('John', 'Doe', 21)
    
In [20]:
print(info)
('Gordon', 'Bean', 39)
In [21]:
first, last, age = info

print(f'{last}, {first} ({age})')
Bean, Gordon (39)

We access the pieces of a tuple using unpacking.

The first item in the tuple is assigned to the first variable, the second item to the second variable, etc.

🖌 Multiple Return¶

In [22]:
def get_number_pair():
    first = int(input('First number: '))
    second = int(input('Second number: '))
    return first, second
In [23]:
print('--First pair--')
pair = get_number_pair()
print(pair)
a, _ = pair


print('--Second pair--')
x, y = get_number_pair()

if a + b > x + y:
    print('The first pair is bigger')
else:
    print('The first pair is NOT bigger')
--First pair--
First number: 12
Second number: 8
--Second pair--
First number: 9
Second number: 9
The first pair is bigger

🖌 list of tuple¶

In [24]:
students = [
    # First name, last name, Hometown
    ('Sally', 'Hernandez', 'Nantucket'),
    ('Shawn', 'Wu', 'Provo'),
    ('Seth', 'de Souza', 'Carlsbad'),
    ('Sarah', 'Ulbrecht', 'Buffalo')
]
In [25]:
for first, last, home in students:
    print(f'{first} {last} is from {home}')
Sally Hernandez is from Nantucket
Shawn Wu is from Provo
Seth de Souza is from Carlsbad
Sarah Ulbrecht is from Buffalo
In [26]:
for student in students:
    first, last, home = student
    print(f'{first} {last} is from {home}')
Sally Hernandez is from Nantucket
Shawn Wu is from Provo
Seth de Souza is from Carlsbad
Sarah Ulbrecht is from Buffalo
In [27]:
for first, last, home in students:
    print(f'{first} {last} is from {home}')
Sally Hernandez is from Nantucket
Shawn Wu is from Provo
Seth de Souza is from Carlsbad
Sarah Ulbrecht is from Buffalo

registration.py¶

  • get_participant
    • Returns None to communicate "no person"
    • Returns a tuple of (first, last, age)
  • register_participants
    • If the participant is None, we're done adding participants
    • Delegates the logic of what information is required to get_participant
  • print_participants
    • Uses unpacking to get the first name, last name, and age of each participant (one at a time)

👩🏻‍🎨 meal_planner.py¶

Write a program that builds up a meal plan for several days.

An individual meal needs a grain, vegetable, and fruit.

Allow the user to plan out as many meals as they want.

Then print out the planned meals.

Plan a meal
Grain: rice
Vegetable: broccoli
Fruit: strawberry
Plan a meal
Grain: pasta
Vegetable: peas
Fruit: cranberry
Plan a meal
Grain: bread
Vegetable: carrots
Fruit: apples
Plan a meal
Grain: 

You planned 3 meals: Grain: rice, Veggie: broccoli, Fruit: strawberry Grain: pasta, Veggie: peas, Fruit: cranberry Grain: bread, Veggie: carrots, Fruit: apples

Key Ideas¶

  • None
  • tuple
  • Tuple unpacking
  • Multiple return
  • Lists of tuples